package ctm.javacode;

import java.io.* ; 
import java.util.*;
import java.sql.*;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/editData.do")

public class editData extends HttpServlet {

	private final String SUCCESS_VIEW = "main.jsp"; 
	private final String ERROR_VIEW = "editDataError.jsp"; 
	
	protected void doPost(HttpServletRequest request,
								HttpServletResponse response )
								throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		HttpSession session = request.getSession();
		String nextpage = ERROR_VIEW;
		String name = (String) session.getAttribute("login");
		String UserName = request.getParameter("username");
		String mail = request.getParameter("mail");
		String weight = request.getParameter("weight");
		String height = request.getParameter("height");
		String education = request.getParameter("education");
		String marriage = request.getParameter("marriage");
		String job = request.getParameter("job");
		String liveWith = request.getParameter("liveWith");
		
		List<String> errors = new ArrayList<String>() ;
			
		if (isInvalidMail( mail ))
			errors.add("E-mail格式錯誤");
		if(isInvaliduserName(UserName))
			errors.add("使用者名稱格式不正確");
		if ( errors.isEmpty() ) {
			nextpage = SUCCESS_VIEW ;
			try {
				if ( !updateUserData(mail,UserName,weight,height,name,education,marriage,job,liveWith ) ) {
					request.setAttribute("errors", errors) ;
				} // end if
			} catch (NamingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} // end if
		else {
			request.setAttribute("errors", errors) ;
		} // end else
			
	
		request.getRequestDispatcher(nextpage)
		   .forward(request, response);		
	} // end doPost

	private boolean updateUserData(String mail,String username,String weight,String height,String name,String education,String marriage,String job,String liveWith) 
			throws NamingException, SQLException {
		dbBean db = new dbBean( "jdbc/ctmpllab" );
		String sqlQuery_testPW = "SELECT ACCOUNT FROM accounttable WHERE ACCOUNT = '" + name + "'" ;

		
		String sqlQuery_updateMail = "UPDATE accounttable SET EMAIL = '" + mail + "' WHERE ACCOUNT = '" + name + "'";
		String sqlQuery_updateName = "UPDATE accounttable SET name = '" + username + "' WHERE ACCOUNT = '" + name + "'";
		String sqlQuery_updateWeight = "UPDATE accounttable SET weight = '" + weight + "' WHERE ACCOUNT = '" + name + "'";
		String sqlQuery_updateHeight = "UPDATE accounttable SET height = '" + height + "' WHERE ACCOUNT = '" + name + "'";
		String sqlQuery_updateradio = "UPDATE accounttable SET education = '" + education +"',marriage ='"+marriage+"',job='"+job+"',liveWith='"+liveWith+"' WHERE ACCOUNT = '" + name + "'";
		
	    db.Connected();
		ResultSet rs = db.ExcuteQuery(sqlQuery_testPW); ;
		rs.next(); 
			
	
		if ( mail != null && ! mail.equals("") )
			db.ExcuteUpdate(sqlQuery_updateMail);
		if(!username.equals(""))
			db.ExcuteUpdate(sqlQuery_updateName);
		if(!weight.equals(""))
			db.ExcuteUpdate(sqlQuery_updateWeight);
		if(!height.equals(""))
			db.ExcuteUpdate(sqlQuery_updateHeight);
		db.ExcuteUpdate(sqlQuery_updateradio);
		return true;
	}

	private boolean isInvalidMail(String mail) {
		return (! mail.matches("^[_a-zA-Z0-9-]+([.]" + 
		"[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+([.][a-zA-Z0-9-]+)*$") ) || mail == null || mail.equals("") ;
	}

	private boolean isInvaliduserName(String userName) {
		return userName.equals("");
	}
	
	
} // end class
